package com.farm.farming.dao;

import com.farm.farming.pojo.Cust_role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;

@Repository
@Transactional
public interface Cust_roleMapper extends JpaRepository<Cust_role,Integer>, JpaSpecificationExecutor<Cust_role> {


    @Query(value = "SELECT * FROM t_cust_role tr,t_cust_permission tp ,t_cust_role_permission trp WHERE tr.id=trp.role_id  AND tp.id =trp.permission_id AND tr.role =?1",nativeQuery = true)
    public Cust_role findByrole(String role);


    @Query(value = "insert into  t_cust_role_menu values (?1,?2)",nativeQuery = true)
    @Modifying
    void saveRoleIdAndMenuId(Integer id, Integer menuId);

    @Query(value = "insert into t_cust_role_permission values (?1,?2)",nativeQuery = true)
    @Modifying
    void saveRoleIdAndPermissionId(Integer permissionId, Integer id);

    @Query(value = "select count(0) from t_cust_user_role where role_id=?1",nativeQuery = true)
    int findTcusturByroleId(Integer id);

    @Query(value = "delete from t_cust_role_permission where role_id=?1",nativeQuery = true)
    @Modifying
    void deleteTcustRolePermissionByroleId(Integer id);

    @Query(value = "delete from t_cust_role_menu where role_id=?1",nativeQuery = true)
    @Modifying
    void deleteTcustRoleMenuByroleId(Integer id);

}
